<?php
class Group {

	var $id_group;
	var $name;
	var $slug;
	var $creation_date;
	
	var $members;

	var $_table_name;
	
	var $_errmsg;
	var $_site;
	var $_db;
	
	/////////////////////////////////////////////////
	// Static member methods
	
	function Group($id_group = -1) {
		global $site;
		$this->_table_name = "groups";
		$this->_site = $site;
		$this->_db = new mysqlconn(HOST,USER,PASS,DB);
		
		if ($id_user != -1) {
			$this->load($id_group);
		}		
	}
	
	function exists($id_group) {
		$db = new mysqlconn(HOST, USER, PASS, DB);
		$db->query("SELECT COUNT(1) FROM ".PREFIX."groups WHERE id_group = '?'", $id_group);
		return $db->getfield();
	}

	function exists_by_slug($slug) {
		$db = new mysqlconn(HOST, USER, PASS, DB);
		$db->query("SELECT COUNT(1) FROM ".PREFIX."groups WHERE slug = '?'", $slug);
		return $db->getfield();
	}

	/////////////////////////////////////////////////
	// Instance member methods
	
	function load ($id_group) {
		
		// Test the group exists before doing wrong
		if (!Group::exists($id_group)) {
			$this->_errmsg = $this->_site->lang('The group doesn\'t exists');
			return 0;
		}
		
		$this->_db->query(
			"SELECT
				*
			FROM
				".PREFIX."groups
			WHERE
				id_group = '?'", 
			$id_group
		);
		
		// Bring it. If it doesn't come, return 0, that's an error
		if (!$n = $this->_db->getarray ()) {
			$this->_errmsg = $this->_site->lang('There was a problem with the database, please contact the site admin');
			return 0;
		}

		foreach ($n as $n_key => $n_value) {
			$this->$n_key = $n_value;
		}
		
		return 1;
	
	}

	function load_by_slug ($slug) {
		
		if (!Group::exists_by_slug($slug)) {
			$this->_errmsg = $this->_site->lang('The group doesn\'t exists');
			return 0;
		}
		
		$this->_db->query(
			"SELECT
				*
			FROM
				".PREFIX."groups
			WHERE
				slug = '?'", 
			$slug
		);
		
		// Bring it. If it doesn't come, return 0, that's an error
		if (!$n = $this->_db->getarray ()) {
			$this->_errmsg = $this->_site->lang('There was a problem with the database, please contact the site admin');
			return 0;
		}
		
		foreach ($n as $n_key => $n_value) {
			$this->$n_key = $n_value;
		}
				
		return 1;
		
	}
	
	function save() {
	
		// Before doing anything we think we're doing, we must validate the data.
		// TODO
		
		if ($this->id_group) {
			
			if (!Group::exists($this->id_group)) {
				$this->_errmsg = $this->_site->lang("The Group object has an id, but there's no user with that id!");
				return 0;
			}
			
			$this->_db->query(
				"UPDATE 
					".PREFIX."groups 
				SET 
					name = '?',
					slug = '?'
				WHERE id_group = '?'",
					$this->name,
					$this->slug,
					$this->id_group);
		} else {
			
			$this->_db->query(
				"INSERT INTO
					".PREFIX."groups 
				(name, slug)
				VALUES
				('?', '?')",
					$this->name,
					$this->slug);

		}
	}
	
	function get_id() {
		return $this->id_group;
	}
	
	// Membresy methods
	/**
	* This is the first function that supposes that classes within the core ar done in the same way
	* implementing some of the same functions. As for example: get_id(). 
	*/
	function add_membresy($object, $level) {
		
		$membresy = new Membresy();
		$membresy->id_member = $this->id_user;
		$membresy->member_table = $this->_table_name;
		$membresy->id_element = $object->get_id();
		$membresy->element_table = $object->_table_name;
		$membresy->level = $level;
		
		if ($membresy->save()) {
			return 1;
		} else {
			$this->_errmsg = $membresy->_errmsg;
			return 0;
		}
		
	}
	
	/**
	* Generic way of "add_membresy". It checks if exists, if not calls "add..." */
	function set_membresy($object, $level) {
	
		if ($this->get_level_in($object)) {
		
			$this->_db->query(
				"SELECT
					id_membresy
				FROM 
					".PREFIX."membresies
				WHERE 
					id_member = '?
					AND member_table = '?'
					AND id_element = '?'
					AND element_table = '?'",
					$this->get_id,
					$this->_table_name,
					$object->get_id(),
					$object->_table_name
			);
			$membresy = new Membresy($this->_db->getfield());	
			$membresy->level = $level;
			if ($membresy->save()) {
				return 1;
			} else {
				$this->_errmsg = $membresy->_errmsg;
				return 0;
			}
			
		} else {
	
			return $this->add_membresy($object);

		}
		
	}
	
	/** Useful function. Knowing if it's direct member allows to easily get its level */
	function get_level_in($object) {
		
		$this->_db->query(
			"SELECT
				level
			FROM 
				".PREFIX."membresies
			WHERE 
				id_member = '?
				AND member_table = '?'
				AND id_element = '?'
				AND element_table = '?'",
				$this->get_id(),
				$this->_table_name,
				$object->get_id(),
				$object->_table_name
		);
		return $this->_db->getfield();
		
	}
	
	function get_membresies() {
		if (!$this->membresies) {
			
			$this->_db->query(
				"SELECT
					id_membresy
				FROM
					".PREFIX."membresies
				WHERE
					id_member = '?'
					AND member_table = '?'",
					$this->get_id(),
					$this->_table_name
			);
			
			$this->membresies = array();
			
			while ($id = $this->_db->getfield()) {
				$this->membresies[] = new Membresy($id);
			}

		}
		
		return $this->membresies;
		
	}
	
}
?>